package org.gnyxy.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.gnyxy.dto.CartNumberDTO;
import org.gnyxy.entity.Cart;
import org.gnyxy.entity.Material;
import org.gnyxy.vo.CartVO;
import org.gnyxy.vo.CartWithInfoVO;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

@Mapper
public interface CartMapper {
//    static void insertBatch(List<Cart> shoppingCartList) {
//    }

    //    @Insert("insert into cart(user_id, dish_id, material_id, number, deleted) values " +
//            "(#{userId},#{dishId},#{materialId},#{number},#{deleted})")
    void insert(Cart cart);

    @Select("select * from cart where deleted = 0 and user_id = #{userId}")
    List<Cart> getByUserId(Long userId);

    @Select("select id from cart where deleted = 0 and user_id = #{userId} and dish_id is not null")
    List<Long> getIdsByUserIdAndDish(Long userId);
//    @Select("select * from cart where ")
//    List<CartVO> getCartVOByUserId(Long userId);

    CartWithInfoVO getCartVOById(Long cartId);

    void update(CartNumberDTO cartNumberDTO);

    @Select("select * from cart where deleted = 0 and id = #{id}")
    Cart getById(Long id);

    void delete(Cart cart);

    @Select("select id from cart where deleted = 0 order by id desc")
    Long getIdByUserId(Long userId);

    @Select("select number from cart where deleted = 0 and dish_id = #{dishId}")
    BigDecimal getNumberByDishId(Long dishId);

    @Select("select MAX(id) from cart")
    Long getMaxId();

    @Select("select number from cart where deleted = 0 and material_id = #{materialId}")
    BigDecimal getNumberByMaterialId(Long materialId);

    @Update("update cart set deleted = 1 where dish_id = #{dishId} and user_id = #{userId}")
    void deleteByDishId(Long dishId, Long userId);

    @Update("update cart set deleted = 1 where material_id = #{materialId} and user_id = #{userId}")
    void deleteByMaterialId(Long materialId,Long userId);

    void deleteByIds(List<Long> cartIds);

    @Select("select id from cart where deleted = 0 and dish_id = #{dishId}")
    Long getIdByDishId(Long dishId);

//    @Select("select id from cart where deleted = 0 and material_id = #{materialId}")
//    Long getIdByMaterialId(Long materialId);

    @Select("select number from cart where deleted = 0 and user_id = #{userId} and dish_id = #{dishId}")
    BigDecimal getNumberByDishIdAndUserId(Long dishId, Long userId);
    @Select("select number from cart where deleted = 0 and user_id = #{userId} and material_id = #{materialId}")
    BigDecimal getNumberByMaterialIdAndUserId(Long materialId, Long userId);

    @Select("select id from cart where deleted = 0 and user_id = #{userId} and material_id = #{materialId}")
    Long getIdByMaterialIdAndUserId(Long materialId, Long userId);

//    void deleteByMaterialIdAndUserId(Long materialId, Long userId);
}
